home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue57 / alfresco / tstMonth.dpr next >
Encoding:
Text File  |  2000-04-02  |  1.4 KB  |  67 lines

  1. program tstMonth;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   Windows,
  7.   SysUtils;
  8.  
  9. function CalcDaysInMonth1(Month, Year : integer) : integer;
  10. begin
  11.   if (Month = 4) or (Month = 6) or (Month = 9) or (Month = 11) then
  12.     Result := 30
  13.   else if (Month = 2) then begin
  14.     if (((Year div 4) = 0) and
  15.         (((Year div 100) <> 0) or ((Year div 400) = 0))) then
  16.       Result := 29
  17.     else
  18.       Result := 28
  19.   end
  20.   else
  21.     Result := 31;
  22. end;
  23.  
  24. function CalcDaysInMonth2(Month, Year : integer) : integer;
  25. const
  26.   DaysInMonth : array [1..12] of integer =
  27.     (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  28. begin
  29.   Result := DaysInMonth[Month];
  30.   if (Month = 2) then begin
  31.     if (((Year div 4) = 0) and
  32.         (((Year div 100) <> 0) or ((Year div 400) = 0))) then
  33.       Result := 29;
  34.   end;
  35. end;
  36.  
  37. var
  38.   i : integer;
  39.   M, Y : integer;
  40.   StartTime : DWORD;
  41. begin
  42.   writeln('testing algorithm 1...');
  43.   StartTime := GetTickCount;
  44.   for i := 1 to 10000 do begin
  45.     for Y := 1600 to 3000 do begin
  46.       for M := 1 to 12 do begin
  47.         CalcDaysInMonth1(M, Y);
  48.       end;
  49.     end;
  50.   end;
  51.   writeln('time taken: ', GetTickCount - StartTime);
  52.  
  53.   writeln('testing algorithm 2...');
  54.   StartTime := GetTickCount;
  55.   for i := 1 to 10000 do begin
  56.     for Y := 1600 to 3000 do begin
  57.       for M := 1 to 12 do begin
  58.         CalcDaysInMonth2(M, Y);
  59.       end;
  60.     end;
  61.   end;
  62.   writeln('time taken: ', GetTickCount - StartTime);
  63.  
  64.  
  65.   readln;
  66. end.
  67.